package com.xxl.controller;

import com.xxl.dto.User;
import com.xxl.util.FieldConst;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author xxl
 * @date 2023/3/15
 */
@Order(4562)
@Controller
@Tag(name = "HomeController",description = "用于跳转到主页的")
public class HomeController {

    @Resource
    HttpServletRequest request;
    @Resource
    HttpServletResponse response;

    @Operation(summary = "去往主页的的方法")
    @ApiResponses(
            @ApiResponse(description = "返回视图")
    )
    @Parameters(
            @Parameter(name = "isRemember",description = "true就开启记住我功能，false则不开启")
    )
    @GetMapping("/")
    public String toIndex(boolean isRemember) {
        //如果为true就是使用了记住我功能
        if(isRemember){
            System.out.println("使用了记住我功能");
            for (Cookie cookie : request.getCookies()) {
                if (cookie.getName().equals(FieldConst.COOKIE_ID)) {
                    Cookie cookie1 = new Cookie(FieldConst.COOKIE_ID,cookie.getValue());
                    cookie1.setMaxAge(3600);
                    response.addCookie(cookie1);
                }
            }
        }
        return "/index";
    }
    @ResponseBody
    @GetMapping("/hello/{username}")
    @Operation(summary = "测试接口",description = "用于测试的接口")
    @Parameter(name = "username",description = "用户姓名不是必须的")
    @ApiResponse(description = "返回suer",responseCode = "200")
    public User hello(@PathVariable(required = false) String username){
        User user = new User("hello","123","admin");
        if (!StringUtils.isEmpty(username)) {
            user.setName(username);
        }
        return user;
    }


}
